<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-button v-if="route_add" class="filter-item" size="small" style="margin-left: 10px;" @click="handleCreate"
                 type="primary"
                 icon="edit">添加
      </el-button>
    </div>

    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="加载中..." border fit
              highlight-current-row style="width: 99%" size="small">

      <el-table-column align="center" label="序号">
        <template slot-scope="scope">
          <span>{{scope.row.id}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="URI">
        <template slot-scope="scope">
          <span>{{scope.row.uri}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Order">
        <template slot-scope="scope">
          <span>{{scope.row.order}} </span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="断言">
        <template slot-scope="scope">
          <span>{{scope.row.predicates}} </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="过滤器">
        <template slot-scope="scope">
          <span>{{scope.row.filters}} </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200" v-if="route_edit || route_del">
        <template slot-scope="scope">
          <el-button size="small" type="primary" v-if="route_edit" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="small" type="primary" v-if="route_del" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.page" :page-sizes="[10,30,50,100]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item v-if="dialogStatus == 'update'" label="ID" prop="id">
          <el-input v-model="form.id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item v-if="dialogStatus == 'create'" label="ID" prop="id">
          <el-input v-model="form.id" placeholder="请输入ID"></el-input>
        </el-form-item>
        <el-form-item label="URI" prop="uri">
          <el-input v-model="form.uri" placeholder="http://www.baidu.com"></el-input>
        </el-form-item>
        <el-form-item label="Order" prop="order">
          <el-input v-model.number="form.order" type="number" placeholder="越小越优先"></el-input>
        </el-form-item>
        <el-form-item label="断言" prop="predicates">
          <el-input v-model="form.predicates" type="textarea" :autosize="true"
                    placeholder='["Path=/baidu"]'></el-input>
        </el-form-item>
        <el-form-item label="过滤器" prop="filters">
          <el-input v-model="form.filters" type="textarea" :autosize="true"
                    placeholder='["RewritePath=/(?<segment>.*), /baidu/$\\{segment}"]'></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">保存</el-button>
        <el-button v-else type="primary" @click="update('form')">更新</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {fetchList, getObj, addObj, deleteObj, putObj} from '@/api/route'
  import waves from "@/directive/waves/index.js"; // 水波纹指令
  import {mapGetters} from 'vuex'

  export default {
    name: 'route',
    directives: {
      waves
    },
    data() {
      return {
        tableKey: 0,
        list: null,
        dialogStatus: "",
        dialogFormVisible: false,
        textMap: {
          update: "编辑",
          create: "创建"
        },
        methodOptions: ['GET', 'POST', 'PUT', 'DELETE'],
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 10
        },
        labelPosition: 'right',
        form: {
          id: undefined,
          uri: undefined,
          order: undefined,
          predicates: undefined,
          filters: undefined
        },
        rules: {
          id: [
            {
              required: true,
              message: "请输入ID",
              trigger: "blur"
            },
            {
              min: 1,
              max: 100,
              message: "长度在 1 到 100 个字符",
              trigger: "blur"
            }
          ],
          uri: [
            {
              required: true,
              message: "请输入URI",
              trigger: "blur"
            }
          ],
          order: [
            {
              required: true,
              message: "请输入Order",
              trigger: "blur"
            }
          ],
          filters: [
            {
              required: true,
              message: "请输入过滤器",
              trigger: "blur"
            }
          ],
        },
        route_add: false,
        route_edit: false,
        route_del: false
      }
    },
    filters: {},
    created() {
      this.getList()
      this.route_add = this.permissions['route_add']
      this.route_edit = this.permissions['route_edit']
      this.route_del = this.permissions['route_del']
    },
    computed: {
      ...mapGetters([
        'elements',
        'permissions'
      ])
    },
    methods: {
      getList() {
        this.listLoading = true;
        fetchList(this.listQuery).then(response => {
          this.list = response.data.content;
          this.total = response.data.totalElements;
          this.listLoading = false;
        })
      },
      handleFilter() {
        this.listQuery.page = 1;
        this.getList();
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      },
      handleUpdate(row) {
        getObj(row.id).then(response => {
          let predicates = response.data.predicates
          let filters = response.data.filters
          response.data.predicates = predicates ? JSON.stringify(predicates, null, 2) : ''
          response.data.filters = filters ? JSON.stringify(filters, null, 2) : ''
          this.form = response.data
          this.dialogFormVisible = true;
          this.dialogStatus = "update";
        })
      },
      handleCreate() {
        this.resetForm()
        this.dialogStatus = "create";
        this.dialogFormVisible = true;
      },
      update(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
          if (valid) {
            putObj(this.form).then(() => {
              this.dialogFormVisible = false;
              this.getList();
              this.$notify({
                title: "成功",
                message: "修改成功",
                type: "success",
                duration: 2000
              });
            });
          } else {
            return false;
          }
        });
      },
      create(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
          if (valid) {
            addObj(this.form).then(() => {
              this.dialogFormVisible = false;
              this.getList();
              this.$notify({
                title: "成功",
                message: "创建成功",
                type: "success",
                duration: 2000
              });
            });
          } else {
            return false;
          }
        });
      },
      handleDelete(row) {
        deleteObj(row.id)
          .then(() => {
            this.getList();
            this.$notify({
              title: "成功",
              message: "删除成功",
              type: "success",
              duration: 2000
            });
          });
      },
      cancel(formName) {
        this.dialogFormVisible = false
        this.$refs[formName].resetFields();
      },
      resetForm() {
        this.form = {
          id: undefined,
          uri: undefined,
          order: undefined,
          predicates: undefined,
          filters: undefined
        }
      }
    }
  }
</script>

